<?php

namespace addons\Contract\merchant\controllers;

use Yii;
use yii\helpers\Json;
use common\traits\Curd;
use common\helpers\ExcelHelper;
use common\models\base\SearchModel;
use common\enums\StatusEnum;
use addons\Contract\common\models\Contract;

class DefaultController extends BaseController
{
    use Curd;

    public $modelClass = Contract::class;

    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'relations' => [], // 关联 member 表的 nickname 字段
            'partialMatchAttributes' => ['title'], // 模糊查询，注意 member_nickname 为关联表的别名 表名_字段
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $dataProvider->query->andFilterWhere([Contract::tableName().'.merchant_id' => $this->getMerchantId()]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,

        ]);
    }

    public function actionAjaxEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        $model->covers = Json::decode($model->covers);
        $model->merchant_id =  $this->getMerchantId();

        // ajax 校验
        $this->activeFormValidate($model);
        if ($model->load(Yii::$app->request->post())) {
            !empty($model->covers) && $model->covers = Json::encode($model->covers);

            return $model->save()
                ? $this->redirect(['index'])
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
        }

        return $this->renderAjax('ajax-edit', [
            'model' => $model,
        ]);
    }
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        $model->covers = Json::decode($model->covers);
        $model->merchant_id =  $this->getMerchantId();
        if ($model->load(Yii::$app->request->post())) {
            !empty($model->covers) && $model->covers = Json::encode($model->covers);
            return $model->save()
                ? $this->redirect(['index'])
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
        }

        return $this->render('edit', [
            'model' => $model,
        ]);
    }
    public function actionCreate()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        $model->covers = Json::decode($model->covers);
        $model->merchant_id =  $this->getMerchantId();
        if ($model->load(Yii::$app->request->post())) {
            !empty($model->covers) && $model->covers = Json::encode($model->covers);
            return $model->save()
                ? $this->redirect(['index'])
                : $this->message($this->getError($model), $this->redirect(['index']), 'error');
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }
    public function actionExport()
    {
        $header = [
            ['合同类型', 'cate_id', 'selectd', \addons\Contract\common\enums\ContractEnum::getMap()],
            ['合同编号', 'serial'],
            ['合同名称', 'title'],
            ['甲方', 'PartyA'],
            ['乙方', 'PartyB'],
            ['签定日期', 'date'],
        ];
        $list = Contract::find()->select('*')
            ->andWhere(['status' => StatusEnum::ENABLED,'merchant_id'=>Yii::$app->user->identity->merchant_id])
            ->asArray()
            ->all();
        return ExcelHelper::exportData($list, $header);
    }
    public function actionDelete($id)
    {
        if ($this->findModel($id)->delete()) {
            return $this->message("删除成功", $this->redirect(['index']));
        }
        return $this->message("删除失败", $this->redirect(['index']), 'error');
    }

    protected function findModel($id)
    {
        if (empty($id) || empty(($model = $this->modelClass::findOne(['id' => $id, 'merchant_id' => $this->getMerchantId()])))) {
            $model = new $this->modelClass;
            return $model->loadDefaultValues();
        }

        return $model;
    }
}
